var Z = Object.defineProperty;
var ee = (t, e, n) => e in t ? Z(t, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : t[e] = n;
var b = (t, e, n) => (ee(t, typeof e != "symbol" ? e + "" : e, n), n);
import { onMounted as te, onActivated as ne, onDeactivated as le, onUnmounted as re } from "vue";
const g = {
  UP: {
    code: ["ArrowUp", 87, 19, 38],
    preventDefault: !0,
    callback: null
  },
  DOWN: {
    code: ["ArrowDown", 83, 40, 20, 47],
    preventDefault: !0,
    callback: null
  },
  LEFT: {
    code: ["ArrowLeft", 65, 29, 21, 37],
    preventDefault: !0,
    callback: null
  },
  RIGHT: {
    code: ["ArrowRight", 68, 22, 32, 39],
    preventDefault: !0,
    callback: null
  },
  ENTER: {
    code: ["Enter", "NumpadEnter", 13, 73, 66, 23, 1],
    preventDefault: !0,
    callback: null
  },
  // BACK: {
  //   code: ["Backspace", 4, 27, 8],
  //   preventDefault: !0,
  //   callback: null
  // },
  PAGEACTION: {
    code: ["PageUp", "PageDown", 33, 34],
    preventDefault: !0,
    callback: null
  },
  NUMBER: {
    code: [
      "Digit1",
      "Digit2",
      "Digit3",
      "Digit4",
      "Digit5",
      "Digit6",
      "Digit7",
      "Digit8",
      "Digit9",
      "Digit0",
      "Numpad1",
      "Numpad2",
      "Numpad3",
      "Numpad4",
      "Numpad5",
      "Numpad6",
      "Numpad7",
      "Numpad8",
      "Numpad9",
      "Numpad0",
      49,
      50,
      51,
      52,
      53,
      54,
      55,
      56,
      57,
      48,
      96,
      97,
      98,
      99,
      100,
      101,
      102,
      103,
      104,
      105
    ],
    preventDefault: !1,
    callback: null
  }
}, se = () => g, ue = (t, e, n = null, l = !1) => {
  g[t] = {
    code: [...new Set(e)],
    preventDefault: l,
    callback: n
  };
}, oe = (t) => ["UP", "DOWN", "LEFT", "RIGHT", "ENTER", "BACK"].includes(t) ? !1 : delete g[t], ie = (t, e = null) => {
  g[t] ? g[t].callback = e : console.error(`action: ${t} 不存在`);
}, fe = (t, e) => {
  g[t] ? g[t].preventDefault = e : console.error(`action: ${t} 不存在`);
}, ce = (t, e) => {
  g[t] ? g[t].code = [
    .../* @__PURE__ */ new Set([...g[t].code, ...e])
  ] : console.error(`action: ${t} 不存在`);
}, pe = (t, e) => {
  const n = [...new Set(e)];
  return g[t] ? (n.forEach((l) => {
    const u = g[t].code.findIndex(
      (f) => f == l
    );
    u > -1 && g[t].code.splice(u, 1);
  }), !0) : (console.error(`action: ${t} 不存在`), !1);
}, ae = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  addCodeToAction: ce,
  getCurrentKeyActions: se,
  keyActions: g,
  removeAction: oe,
  removeCodeFromAction: pe,
  setAction: ue,
  setActionCallback: ie,
  setActionPreventDefault: fe
}, Symbol.toStringTag, { value: "Module" })), F = (t) => {
  const e = t.childNodes;
  let n = [];
  for (let l = 0; l < e.length; l++) {
    const u = e[l];
    u.nodeType == 1 && (n.push(u), n = [...n, ...F(u)]);
  }
  return n;
}, j = (t = "") => t + Math.random().toString(36).replace(/[^a-z]+/g, "").substring(0, 10), B = (t) => t.offsetParent == null, c = (...t) => {
  I.debug && console.log(
    "%cvuEPG",
    "color:white; background: linear-gradient(270deg, skyblue, pink); padding: 5px 8px; border-radius: 15px 15px 0 15px",
    ...t
  );
}, E = 0.05, G = (t, e = s.currentItem) => {
  if (e == null)
    return null;
  let n = !1;
  const l = k(e.el);
  c("获取父元素", l);
  let u;
  if (l == null ? (u = z(), n = !0, c("父元素获取失败，使用全局父元素")) : u = l.children, c("该层所有子元素", u), n && u.length == 1)
    return c("已查询到最顶层且只有该元素", u), null;
  const f = de(
    u,
    e,
    t
  );
  if (c("符合要求的元素", f), f.length == 0)
    return n ? (c("当前已是最顶层，停止向上寻找"), null) : (c(
      "当前符合坐标要求的元素不存在，尝试使用上层作为当前元素",
      k(e.el)
    ), G(
      t,
      k(e.el)
    ));
  const p = he(f, e, t);
  return c("当前获取的最佳目标", p), p ?? (n ? (c("当前已是最顶层，停止向上寻找"), null) : (c(
    "在符合坐标要求的元素中找不到可移动元素，尝试使用上层作为当前元素",
    k(e.el)
  ), G(
    t,
    k(e.el)
  )));
}, de = (t, e, n) => {
  const l = [], u = e == null ? void 0 : e.getRect();
  return t.filter((p) => p != e).forEach((p) => {
    if (B(p.el))
      return;
    const d = p.getRect();
    n == "down" ? u.top < d.top && l.push(p) : n == "right" ? u.left < d.left && l.push(p) : n == "up" ? u.bottom > d.bottom && l.push(p) : n == "left" && u.right > d.right && l.push(p);
  }), l;
}, he = (t, e, n) => n == "down" ? ge(t, e) : n == "right" ? be(t, e) : n == "up" ? me(t, e) : n == "left" ? Ie(t, e) : null, ge = (t, e) => {
  let n = [], l = [];
  const u = e == null ? void 0 : e.getRect();
  let f = null, p = null, d = !1;
  if (t.forEach((i) => {
    const r = i.getRect(), o = u.right - E * u.width < r.left || u.left + E * u.width > r.right, a = u.bottom < r.top;
    !o && f == null ? (f = r.top, n.push(i), d = !0, c("当前元素重叠且无第一个可用元素", n)) : o && p == null ? a && (p = r.top, l.push(i), c("当前元素不重叠且无第一个可用元素", l)) : (o || (d = !0), f > r.top ? o ? a && (l.splice(0, n.length), l.push(i)) : (n.splice(0, n.length), n.push(i)) : o ? p == r.top && a && l.push(i) : f == r.top && n.push(i));
  }), d == !1 && (n = l, c("X轴完全无重叠时，考虑使用非重叠元素", n)), n.length > 0) {
    let i, r = null;
    return n.forEach((o, a) => {
      const h = o.getRect();
      if (a != 0) {
        const m = Math.abs(u.left - h.left);
        m < i && (i = m, r = o);
      } else
        i = Math.abs(u.left - h.left), r = o;
    }), r;
  } else
    return n[0] != null ? n[0] : null;
}, me = (t, e) => {
  let n = [], l = [];
  const u = e == null ? void 0 : e.getRect();
  let f = null, p = null, d = !1;
  if (t.forEach((i) => {
    const r = i.getRect(), o = u.right - E * u.width < r.left || u.left + E * u.width > r.right, a = u.top > r.bottom;
    !o && f == null ? (f = r.bottom, n.push(i), d = !0, c("当前元素重叠且无第一个可用元素", n)) : o && p == null ? a && (p = r.bottom, l.push(i), c("当前元素不重叠且无第一个可用元素", l)) : (o || (d = !0), f < r.bottom ? o ? a && (l.splice(0, n.length), l.push(i)) : (n.splice(0, n.length), n.push(i)) : o ? p == r.bottom && a && l.push(i) : f == r.bottom && n.push(i));
  }), d == !1 && (n = l, c("X轴完全无重叠时，考虑使用非重叠元素", n)), n.length > 0) {
    let i, r = null;
    return n.forEach((o, a) => {
      const h = o.getRect();
      if (a != 0) {
        const m = Math.abs(u.left - h.left);
        m < i && (i = m, r = o);
      } else
        i = Math.abs(u.left - h.left), r = o;
    }), r;
  } else
    return n[0] != null ? n[0] : null;
}, be = (t, e) => {
  let n = [], l = [];
  const u = e == null ? void 0 : e.getRect();
  let f = null, p = null, d = !1;
  if (t.forEach((i) => {
    const r = i.getRect(), o = u.top + E * u.height > r.bottom || u.bottom - E * u.height < r.top, a = u.right < r.left;
    !o && f == null ? (f = r.left, n.push(i), d = !0, c("当前元素重叠且无第一个可用元素", n)) : o && p == null ? a && (p = r.left, l.push(i), c("当前元素不重叠且无第一个可用元素", l)) : (o || (d = !0), f > r.left ? o ? a && (l.splice(0, n.length), l.push(i)) : (n.splice(0, n.length), n.push(i)) : o ? p == r.left && a && l.push(i) : f == r.left && n.push(i));
  }), d == !1 && (n = l, c("Y轴完全无重叠时，考虑使用非重叠元素", n)), n.length > 0) {
    let i, r = null;
    return n.forEach((o, a) => {
      const h = o.getRect();
      if (a != 0) {
        const m = Math.abs(u.top - h.top);
        m < i && (i = m, r = o);
      } else
        i = Math.abs(u.top - h.top), r = o;
    }), r;
  } else
    return n[0] != null ? n[0] : null;
}, Ie = (t, e) => {
  let n = [], l = [];
  const u = e == null ? void 0 : e.getRect();
  let f = null, p = null, d = !1;
  if (t.forEach((i) => {
    const r = i.getRect(), o = u.top + E * u.height > r.bottom || u.bottom - E * u.height < r.top, a = u.left > r.right;
    !o && f == null ? (f = r.right, n.push(i), d = !0, c("当前元素重叠且无第一个可用元素", n)) : o && p == null ? a && (p = r.right, l.push(i), c("当前元素不重叠且无第一个可用元素", l)) : (o || (d = !0), f < r.right ? o ? a && (l.splice(0, n.length), l.push(i)) : (n.splice(0, n.length), n.push(i)) : o ? p == r.right && a && l.push(i) : f == r.right && n.push(i));
  }), d == !1 && (n = l, c("Y轴完全无重叠时，考虑使用非重叠元素", n)), n.length > 0) {
    let i, r = null;
    return n.forEach((o, a) => {
      const h = o.getRect();
      if (a != 0) {
        const m = Math.abs(u.top - h.top);
        m < i && (i = m, r = o);
      } else
        i = Math.abs(u.top - h.top), r = o;
    }), r;
  } else
    return n[0] != null ? n[0] : null;
}, s = {
  currentItem: null,
  itemArray: [],
  currentGroup: null,
  groupArray: []
}, I = {
  focusClass: "vuepg-focus",
  defBackHandler: null,
  tempBackHandler: null,
  debug: !1
}, ve = (t) => {
  Object.assign(I, t);
}, M = (t) => {
  Object.assign(I, {
    tempBackHandler: t
  });
}, H = () => {
  Object.assign(I, {
    tempBackHandler: null
  });
}, Ee = (t) => {
  c("注册 onBack 生命周期"), te(() => {
    M(t);
  }), ne(() => {
    M(t);
  }), le(() => {
    H();
  }), re(() => {
    H();
  });
}, ke = () => s.currentItem, ye = () => s.itemArray, Re = () => s.currentGroup, Be = () => s.groupArray, C = () => {
  var t;
  return ((t = s.currentItem) == null ? void 0 : t.focusClass) || I.focusClass;
}, Ae = () => {
  I.tempBackHandler ? (c("调用页面临时 BackHandler"), I.tempBackHandler()) : (c("调用全局默认 BackHandler"), I.defBackHandler && I.defBackHandler());
}, S = (t) => {
  s.itemArray.push(t);
}, _ = (t) => {
  s.itemArray.forEach((e) => {
    e.id === t.id && Object.assign(e, t);
  });
}, $ = (t) => {
  s.groupArray.push(t);
}, K = (t) => {
  s.groupArray.forEach((e) => {
    e.id === t.id && Object.assign(e, t);
  });
}, v = (t) => {
  if (s.itemArray.length == 0)
    return;
  c("-移动逻辑开始-");
  let e = null;
  if (typeof t == "object")
    t.el ? e = t : t.$el ? e = A(t.$el) ?? y(t.$el) : e = A(t) ?? y(t);
  else if (["up", "down", "right", "left"].includes(t)) {
    if (!s.currentItem) {
      c("当前元素不存在，不进行操作");
      return;
    }
    if (s.currentItem && B(s.currentItem.el)) {
      c("当前元素已经隐藏或不存在，不进行操作");
      return;
    }
    e = W(t);
    let n;
    const l = D(s.currentItem);
    if (e != null) {
      if (R(e.el) ? n = e : n = D(e), l !== n && l && l.events[t] != null)
        return l.events[t]();
    } else
      c("无法获取目标元素");
  }
  e ? (T(e.el) ? O(e) : R(e.el) && L(e), c("移动至：", e)) : c("无可用元素"), c("-移动逻辑结束-");
}, O = (t) => {
  var e, n;
  s.currentItem ? (s.currentItem.isFocus = !1, (e = s.currentItem.el) == null || e.classList.remove(C()), s.currentItem.events.blur && s.currentItem.events.blur()) : s.currentItem = s.itemArray[0] ?? null, s.currentItem = t, s.currentGroup = D(s.currentItem), s.currentItem.isFocus = !0, (n = s.currentItem.el) == null || n.classList.add(C()), s.currentItem.events.focus && s.currentItem.events.focus(), t.events.enter && t.events.enter();
}, L = (t) => {
  let e = t.children.find((n) => n.isDefault == !0);
  if (e == null && (e = t.children[0], e == null))
    throw new Error("当前 EPGGroup 不存在 EPGItem");
  R(e.el) ? (t.events.enter && t.events.enter(), L(e)) : O(e);
}, De = () => {
  document.onkeydown = (t) => {
    const e = t.code ? t.code : t.which ? t.which : t.keyCode;
    we(t, e);
  };
}, we = (t, e) => {
  s.itemArray.length || !s.currentItem && (s.currentItem = s.itemArray[0]);
  let n = null;
  for (let l in g)
    if (g[l].code.includes(e)) {
      n = l;
      break;
    }
  if (c("按键KeyCode:", e, "触发事件:", n), n) {
    if (g[n].preventDefault && (t.preventDefault(), c(`事件 ${n} 将阻止默认事件发生`)), ["DOWN", "UP", "LEFT", "RIGHT"].includes(n)) {
      if (s.currentItem == null)
        if (s.itemArray.length != 0)
          s.currentItem = s.itemArray[0];
        else
          return;
      if (B(s.currentItem.el)) {
        c("当前元素已隐藏或不存在，不执行操作");
        return;
      }
      switch (n) {
        case "DOWN":
          s.currentItem.events.down ? s.currentItem.events.down(
            s.currentItem,
            J
          ) : v("down");
          break;
        case "UP":
          s.currentItem.events.up ? s.currentItem.events.up(s.currentItem, q) : v("up");
          break;
        case "LEFT":
          s.currentItem.events.left ? s.currentItem.events.left(
            s.currentItem,
            Y
          ) : v("left");
          break;
        case "RIGHT":
          s.currentItem.events.right ? s.currentItem.events.right(
            s.currentItem,
            V
          ) : v("right");
          break;
      }
    } else if (["ENTER", "BACK"].includes(n))
      switch (n) {
        case "ENTER":
          if (s.currentItem)
            B(s.currentItem.el) ? c("当前元素已隐藏，不处理点击事件") : s.currentItem.el.click();
          else
            return;
          break;
        case "BACK":
          Ae();
          break;
      }
    g[n].callback != null && g[n].callback(e);
  }
}, W = (t) => G(t), A = (t) => s.itemArray.find(
  (e) => e.id === t.dataset.epgItemId
) ?? null, Ge = (t) => F(t.el).filter(
  (e) => e.dataset.epgItemId != null
), y = (t) => s.groupArray.find(
  (e) => e.id === t.dataset.epgGroupId
) ?? null, D = (t) => N(t.el).length > 0 ? s.groupArray.find(
  (e) => {
    var n;
    return e.id === ((n = k(t.el)) == null ? void 0 : n.el.dataset.epgGroupId);
  }
) ?? null : null, w = (t) => {
  const e = t.childNodes;
  let n = [];
  for (let l = 0; l < e.length; l++) {
    const u = e[l];
    if (u.nodeType == 1) {
      const f = X(u);
      f ? n.push(f) : n = [...n, ...w(u)];
    }
  }
  return n;
}, z = () => w(
  document.getElementsByTagName("html")[0]
), X = (t) => {
  let e;
  return R(t) ? e = y(t) : T(t) ? e = A(t) : e = null, e;
}, N = (t) => {
  const e = [];
  let n = t;
  for (; n;)
    n = n.parentElement, n && e.push(n);
  return e;
}, k = (t) => {
  const e = N(t).find(
    (n) => n.dataset.epgGroupId != null
  );
  return e ? y(e) : null;
}, R = (t) => {
  try {
    if (t.dataset.epgGroupId != null)
      return !0;
  } catch {
    return !1;
  }
  return !1;
}, T = (t) => {
  try {
    if (t.dataset.epgItemId != null)
      return !0;
  } catch {
    return !1;
  }
  return !1;
}, Y = () => v("left"), V = () => v("right"), q = () => v("up"), J = () => v("down");
De();
const Q = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  currentConfig: I,
  dataContainer: s,
  down: J,
  getChild: X,
  getCurrentGroup: Re,
  getCurrentItem: ke,
  getFoucsClass: C,
  getGlobalGroupChildren: z,
  getGroupByHTMLElement: y,
  getGroupByItem: D,
  getGroupChildrenByHTMLElement: w,
  getGroups: Be,
  getItemByHTMLElement: A,
  getItems: ye,
  getItemsByGroup: Ge,
  getParentGroupByHTMLElement: k,
  getParentsByHTMLElement: N,
  getTargetByDirection: W,
  isEPGGroup: R,
  isEPGItem: T,
  left: Y,
  move: v,
  moveToGroup: L,
  moveToItem: O,
  onBack: Ee,
  registerGroup: $,
  registerItem: S,
  right: V,
  setConfig: ve,
  up: q,
  updateGroup: K,
  updateItem: _
}, Symbol.toStringTag, { value: "Module" }));
class P {
  constructor(e, n) {
    /**
     * 当前 EPGItem 的 DOM 元素
     */
    b(this, "el");
    /**
     * 当前元素是否未默认选择的元素
     */
    b(this, "isDefault", !1);
    /**
     * 是否为当前焦点
     */
    b(this, "isFocus", !1);
    /**
     * 当前元素的唯一 ID
     */
    b(this, "id", j());
    /**
     * 当前元素的所有监听器函数
     */
    b(this, "events", {
      click: null,
      focus: null,
      blur: null,
      left: null,
      right: null,
      up: null,
      down: null,
      enter: null
    });
    b(this, "focusClass");
    var l, u, f, p, d, i, r, o;
    this.el = e.el, this.el.dataset.epgItemId ? this.id = this.el.dataset.epgItemId : this.el.dataset.epgItemId = this.id, this.isDefault = !!(n.value && n.value.default), this.isFocus = !1, n.value && n.value.class && n.value.class && (this.focusClass = n.value.class), (l = e.props) != null && l.onUp && (this.events.up = e.props.onUp), (u = e.props) != null && u.onDown && (this.events.down = e.props.onDown), (f = e.props) != null && f.onLeft && (this.events.left = e.props.onLeft), (p = e.props) != null && p.onRight && (this.events.right = e.props.onRight), (d = e.props) != null && d.onBlur && (this.events.blur = e.props.onBlur), (i = e.props) != null && i.onFocus && (this.events.focus = e.props.onFocus), (r = e.props) != null && r.onClick && (this.events.click = e.props.onClick), (o = e.props) != null && o.onEnter && (this.events.click = e.props.onEnter);
  }
  /**
   * 返回当前 EPGItem 的 HTMLElement 的 getBoundingClientRect
   */
  getRect() {
    var e;
    return (e = this.el) == null ? void 0 : e.getBoundingClientRect();
  }
}
class U {
  constructor(e, n) {
    /**
     * 当前组的唯一 ID
     */
    b(this, "id", j());
    /**
     * 当前 EPGGroup 的 DOM 元素
     */
    b(this, "el");
    /**
     * 当前元素的第一层子 EPGItem / EPGGroup
     */
    b(this, "children", []);
    /**
     * 当前元素是否未默认选择的元素
     */
    b(this, "isDefault", !1);
    var l, u, f, p, d;
    this.events = {
      click: null,
      focus: null,
      blur: null,
      left: null,
      right: null,
      up: null,
      down: null,
      enter: null
    }, this.el = e.el, this.el.dataset.epgGroupId ? this.id = this.el.dataset.epgGroupId : this.el.dataset.epgGroupId = this.id, this.updateChildren(), this.isDefault = !!(n.value && n.value.default), (l = e.props) != null && l.onUp && (this.events.up = e.props.onUp), (u = e.props) != null && u.onDown && (this.events.down = e.props.onDown), (f = e.props) != null && f.onLeft && (this.events.left = e.props.onLeft), (p = e.props) != null && p.onRight && (this.events.right = e.props.onRight), (d = e.props) != null && d.onEnter && (this.events.right = e.props.onEnter);
  }
  /**
   * 更新当前元素的首层子 EPGItem / EPGGroup
   */
  updateChildren() {
    return this.children = w(this.el), this.children;
  }
  /**
   * 返回当前 EPGGroup 的 HTMLElement 的 getBoundingClientRect
   */
  getRect() {
    var e;
    return (e = this.el) == null ? void 0 : e.getBoundingClientRect();
  }
}
const x = (t, e, n, l) => ({
  beforeMount: t,
  mounted: e,
  updated: n,
  unmounted: l
}), Le = {
  install(t) {
    const e = (r, o, a) => {
    }, n = (r, o, a) => {
      let h = new U(a, o);
      $(h);
    }, l = (r, o, a) => {
      let h = new U(a, o);
      K(h);
    }, u = (r, o, a) => {
      const h = s.groupArray.findIndex(
        (m) => m.id === r.dataset.epgGroupId
      );
      s.groupArray.splice(h, 1);
    };
    t.directive(
      "epg-group",
      x(e, n, l, u)
    );
    const f = (r, o, a) => {
    }, p = (r, o, a) => {
      let h = new P(a, o);
      S(h);
    }, d = (r, o, a) => {
      let h = new P(a, o);
      _(h);
    }, i = (r, o, a) => {
      const h = s.itemArray.findIndex((m) => (s.currentItem && s.currentItem.id === r.dataset.epgItemId && (c("当前元素已经卸载，移除 CurrentItem"), s.currentItem = null), m.id === r.dataset.epgItemId));
      h != -1 && s.itemArray.splice(h, 1);
    };
    t.directive(
      "epg-item",
      x(f, p, d, i)
    ), t.provide("epg", Q);
  }
}, Ne = () => ({ ...Q, ...ae });
export {
  Le as default,
  Ne as useVuEPG
};
